<?

/*************************************************************************************************************/

function host_cluster_form($id=false) {
    $prefix='add';
    if ($id) {
	$prefix='edit';
	$hc=new HostCluster($id);
    }
    echo "<center>".ucfirst($prefix)." host cluster<br>";
?>
<br>
<form method=POST action=<?=$_SERVER['PHP_SELF']?>>
<input type=hidden name=level value="0">
<?
    if ($id) {
	echo "<input type=hidden name=".$prefix."_cluster_id value=$id>";
    }
?>
<table border=0 align=center width=70%>
    <tr align=center valign=top>
	<td>


PostgreSQL cluster parameters<br><br>
<table border=0 align=center>
    <tr>
	<td>Host Name</td>
	<td><input type=text name=<?=$prefix?>_cluster_hostname required<?=($id ? " value=".$hc->get_field('hostname') : ' placeholder=required')?>></td>
    </tr><tr>
	<td>IP Address</td>
	<td><input type=text name=<?=$prefix?>_cluster_ip_address required<?=($id ? " value=".$hc->get_field('ip_address') : ' placeholder=required')?>></td>
    </tr><tr>
	<td>F.Q.D.N.</td>
	<td><input type=text name=<?=$prefix?>_cluster_fqdn<?=(($id && $hc->get_field('fqdn')) ? " value=\"".$hc->get_field('fqdn').'"' : '')?>></td>
    </tr><tr>
	<td>Db Role</td>
	<td><select name=<?=$prefix?>_cluster_is_master>
<?
    $option_string="";
    foreach (array("Slave"=>'f',"Master"=>'t') as $name=>$val) {
	$option_string.="<option";
	if ($id) {
	    if ($hc->get_field('is_master')==$val) {
		$option_string.=" selected";
	    }
	} else {
	    if ($val=='t') {
		$option_string.=" selected";
	    }
	}
	$option_string.=" value=$val>$name";
    }
    echo $option_string;
?>
	</select>
	</td>
    </tr><tr valign=top>
	<td>Comments</td>
	<td><textarea name=<?=$prefix?>_cluster_spec_comments rows=7 cols=45><?=(($id && $hc->get_field('spec_comments')) ? " value=\"".$hc->get_field('spec_comments').'"' : '')?></textarea></td>
    </tr>

</table>
<?
    if ($id) {
	$params=array();
	foreach (preg_split('/ /',$hc->get_conn_string()) as $param) {
	    list($p,$v)=preg_split('/=/',$param);
	    $params[$p]=$v;
	}
    }
?>
	</td><td>


Connection parameters (<b>libpq</b> library)<br><br>
<table border=0 align=center>
    <tr>
	<td>port</td>
	<td><input type=text name=<?=$prefix?>_cluster_conn_port value=<?=($id ? $params['port'] : '5432') ?>></td>
    </tr><tr>
	<td>dbname</td>
	<td><input type=text name=<?=$prefix?>_cluster_conn_dbname value=<?=($id ? $params['dbname'] : 'postgres') ?>></td>
    </tr><tr>
	<td>user</td>
	<td><input type=text name=<?=$prefix?>_cluster_conn_user value=<?=($id ? $params['user'] : 'postgres') ?>></td>
    </tr><tr>
	<td>password</td>
	<td><input type=password name=<?=$prefix?>_cluster_conn_password value=<?=($id && isset($params['password']) ? $params['password'] : '') ?>>
	</td>
    </tr><tr>
	<td>sslmode</td>
	<td align=center><select name=<?=$prefix?>_cluster_conn_sslmode>
<?
    $opt_string='';
    $sslmode=array('disable','allow','prefer','require','verify-ca','verify-full');
    foreach ($sslmode as $mode) {
	if ($id) {
	    if ($mode == $params['sslmode']) {
		$opt_string.="<option selected>".$mode;
	    } else {
		$opt_string.="<option>".$mode;
	    }
	} else {
	    if ($mode == 'prefer') {
		$opt_string.="<option selected>".$mode;
	    } else {
		$opt_string.="<option>".$mode;
	    }
	}
    }
    echo $opt_string;
?>
	</select>
	</td>
    </tr>
</table>
	
	</td>
    </tr>
</table>
<center>
<?
if ($id) {
    echo "libpq Connection string<br><b>".$hc->get_conn_string(true)."</b>";
}
?>
</center>

<br>
<hr width=70%>
<br>
<center><input type=submit name=<?=$prefix?>_cluster_submit value=Submit></center>
</form>
<?
}

/*************************************************************************************************************/

function edit_database_form($id) {
    $dn=new DatabaseName($id);
    $hc=new HostCluster($dn->get_field('hc_id'));
?>
<center>Database <b><?=$dn->get_field('db_name')?></b> for cluster <i><?=$hc->get_field('hostname')?></i></center><br>
<form method=POST action=<?=$_SERVER['PHP_SELF']?>>
<input type=hidden name=edit_dn__dn_id value=<?=$dn->get_id()?>>
<input type=hidden name=edit_dn__hc_id value=<?=$hc->get_id()?>>
<table border=0 align=center>
    <tr>
	<td>Description</td>
	<td><input type=text name=edit_dn__description value=<?='"'.$dn->get_field('description').'"'?>></td>
    </tr><tr>
	<td>Observable</td>
	<td><select name=edit_dn__observable>
<?
    foreach (array('Observable'=>'t','Suspended'=>'f') as $name=>$val) {
	echo "<option value=".$val;
	if ($val == $dn->get_field('observable')) {
	    echo " selected";
	}
	echo ">".$name."</option>";
    }
?>
	</select></td>
    </tr>
</table>
<br><center>
<input type=submit name=edit_dn__submit value=Save>&nbsp;<input type=reset value=Reset>
</form>
<?
}

/*************************************************************************************************************/

function edit_schema_form($sn_id) {
//    $hc=new HostCluster($hc_id);
    $sn=new SchemaName($sn_id);
    $dn=new DatabaseName($sn->get_field('dn_id'));
?>
<table align=center border=0>
    <tr>
	<td>Database</td>
	<td><b><?=$dn->get_field('db_name')?></b></td>
    </tr><tr>
	<td>Schema</td>
	<td><b><?=$sn->get_field('sch_name')?></b></td>
    </tr>
</table>
<br><br>

<form method=POST action=<?=$_SERVER['PHP_SELF']?>>
<input type=hidden name=edit_sn__sn_id value=<?=$sn->get_id()?>>
<input type=hidden name=edit_sn__dn_id value=<?=$dn->get_id()?>>
<table border=0 align=center>
    <tr>
	<td>Description</td>
	<td><input type=text name=edit_sn__description value=<?='"'.$sn->get_field('description').'"'?>></td>
    </tr><tr>
	<td>Observable</td>
	<td><select name=edit_sn__observable>
<?
    foreach (array('Observable'=>'t','Suspended'=>'f') as $name=>$val) {
	echo "<option value=".$val;
	if ($val == $sn->get_field('observable')) {
	    echo " selected";
	}
	echo ">".$name."</option>";
    }
?>
	</select></td>
    </tr>
</table>
<br><center>
<input type=submit name=edit_sn__submit value=Save>&nbsp;<input type=reset value=Reset>
</form>
<?
}

/*************************************************************************************************************/

function remove_cluster_warn($id) {

    $hc=new HostCluster($id);
    echo "<center><br><br><font color=red size=7>!!! WARNING !!!</font><br><br><font size=5>You are about to delete cluster <b>".$hc."</b></font>";
    echo "<br><br><a href=".$_SERVER['PHP_SELF']."?action=list&content=host_cluster>Cancel</a>";
    for ($i=1;$i<25;$i++) {
	echo "<br>";
    }
    echo "<a href=".$_SERVER['PHP_SELF']."?action=destroy&content=host_cluster&hc_id=".$hc->get_id().">Confirm</a><br>";
}

?>
